package Client;

import java.io.IOException;

import javax.swing.JOptionPane;

import GUI.CustomerMain;
import GUI.CustomerService;
import GUI.MainScreen;
import GUI.ParkingWorker;


import OCSF.*;

/**
 * this class get answer from the server 

 */

public class ClientHandler extends OCSF.AbstractClient {
	public static Object togui;

	
	  
	public ClientHandler(String host, int port) throws IOException {
		super(host, port);
		openConnection();
		
	}


	@SuppressWarnings("unchecked")
	protected void handleMessageFromServer(Object msg) {
		
		
		if(msg instanceof String)
		{
			if(msg.toString().equals("NotInTheSystem"))
				JOptionPane.showMessageDialog(null, "Invalid username or password");
			else if(msg.toString().equals("MultipleConnect")){
				JOptionPane.showMessageDialog(null, "Username already logged in");
			}
			else if(msg.toString().equals("LogoutSuccess")){
				MainScreen.cardLayot.show(MainScreen.card,"Login");
				JOptionPane.showMessageDialog(null, "User logout successfuly");
			}
			else if(msg.toString().equals("worker"))
			{
				ParkingWorker pk=new ParkingWorker();
				MainScreen.card.add("ParkingWorker",pk);
				MainScreen.cardLayot.show(MainScreen.card,"ParkingWorker");
			}
			else if(msg.toString().equals("service"))
			{
				CustomerService cs=new CustomerService();
				MainScreen.card.add("CustomerService",cs);
				MainScreen.cardLayot.show(MainScreen.card,"CustomerService");
			}
			else if(msg.toString().equals("Customer"))
			{
				CustomerMain cm=new CustomerMain();
				MainScreen.card.add("CustomerMain",cm);
				MainScreen.cardLayot.show(MainScreen.card,"CustomerMain");
			}
			else if(msg.toString().equals("OneTimeRequest"))
			{
				JOptionPane.showMessageDialog(null, "One time parking request created successfuly");
				MainScreen.cardLayot.show(MainScreen.card,"CustomerMain");
			}
			
		}
	
	
			
			
			
			
		

	}
		
		

	  public void handleMessageFromGui(Object msg)
	  {
	    try
	    {
	    	sendToServer(msg);
	    }
	    catch(IOException e)
	    {
	 
	       System.out.println("Could not send message to server.  Terminating client.");
	      e.printStackTrace();
	      quit();
	    }
	  }
	  
	  /**
	   * This method terminates the client.
	   */
	  public void quit()
	  {
	    try
	    {
	      closeConnection();
	    }
	    catch(IOException e) {}
	    System.exit(0);
	  }
	}
	//End of ChatClient class

	
	
	
	